import React, {Component} from 'react'
import Taro from '@tarojs/taro'
import {View, Form, Button, Input } from '@tarojs/components'
import {AtCard} from "taro-ui"

import {getStorageSync, setStorageSync, showToast} from '../../utils/common'
import {studentFixMsg} from '../../service/user'
import './index.scss'

type PageStateProps = {}

interface StuAuth {
  props: PageStateProps;
}

class StuAuth extends Component {
  state = {
    edu_class: '',
    edu_account: '',
    edu_password: ''
  }

  componentDidMount() {
  }


  async onSubmit(v) {
    const {id} = getStorageSync('userInfo')
    if (!id) return showToast('请先登录！')
    let params = {
      id: id,
      edu_class: v.detail.value.edu_class,
      edu_account: v.detail.value.edu_account,
      edu_password: v.detail.value.edu_password
    }
    const res = await studentFixMsg(params)
    if(res.code === 1) {
      console.log(res.data);
      showToast('个人认证成功！')
      setStorageSync('userInfo',res.data)
      Taro.navigateBack({
        delta:1
      })
    }
  }

  onReset() {
    this.setState({
      edu_class: '',
      edu_account: '',
      edu_password: ''
    })
  }

  render() {
    return (
      <View className='stu_auth'>
        <AtCard
          note='* 此信息将影响课表及成绩查询，请谨慎填写！'
          title='学生信息认证'
        >
          <Form
            onSubmit={this.onSubmit.bind(this)}
            onReset={this.onReset.bind(this)}
          >
            <View className='stu_input_label'>
              <View>所在班级：</View>
              <Input
                name='edu_class'
                type='text'
                className='stu_input_item'
                placeholder='请输入所在班级'
                value={this.state.edu_class}
              />
            </View>
            <View className='stu_input_label'>
              <View>教务账号：</View>
              <Input
                name='edu_account'
                type='text'
                className='stu_input_item'
                placeholder='请输入教务账号'
                value={this.state.edu_account}
              />
            </View>
            <View className='stu_input_label'>
              <View>教务账号：</View>
              <Input
                name='edu_password'
                type='text'
                password
                className='stu_input_item'
                placeholder='请输入教务密码'
                value={this.state.edu_password}
              />
            </View>

            <Button className='stu_sub_btn' formType='submit'>提交</Button>
            <Button className='stu_reset_btn' formType='reset'>重置</Button>
          </Form>
        </AtCard>

      </View>
    )
  }
}

export default StuAuth
